
function validateFundAllocation(theForm) {

}

function validateSubtaskUpdate(theForm) {
    var reason = "";

    //needs a description
    reason += validateEmpty(theForm.textFieldPercentComplete);
    reason += validateDropdownSet(theForm.subtaskStatusDropdown);

    if (isNaN(theForm.textFieldPercentComplete.value)) {
        reason += "% Complete must be a number between 0-100. \n"
        theForm.textFieldPercentComplete.style.background = 'Yellow';
    }

    var origStatus = document.getElementById("origStatusId");
    var selectedStatus = document.getElementById('subtaskStatusDropdown');
    console.log(origStatus.value);
    console.log(selectedStatus.value);
    if (origStatus.value !== selectedStatus.value) {
        if (validateEmpty(theForm.textFieldComments)) {
            reason += "Status changed, please add comments.";
        }
    }
    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    return true;
}
function validateNonLabor(theForm) {
    var reason = "";
    //needs a description
    reason += validateEmpty(theForm.descriptionTextfield);
    //needs a cost
    reason += validateEmpty(theForm.costTextfield);
    //user has to select a type
    reason += validateDropdownSet(theForm.dropdownType);

    if (isNaN(theForm.costTextfield.value)) {
        reason += "Cost Field must be a number in dollars. \n"
        theForm.costTextfield.style.background = 'Yellow';
    }

    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    return true;
}

function validateContract(theForm) {
    var reason = "";
    //needs a contract number
    reason += validateEmpty(theForm.contractNumberTextfield);
    //needs a cost
    reason += validateEmpty(theForm.amountTextfield);
    //needs a start
    reason += validateEmpty(theForm.popStartTextfield);
    //needs an end
    reason += validateEmpty(theForm.popEndTextfield);

    //needs a COR name
    reason += validateEmpty(theForm.corNameNumberTextfield);
    //needs a COR phone
    reason += validateEmpty(theForm.phoneTextfield);
    //needs a COR email
    reason += validateEmpty(theForm.emailTextfield);

    if (isNaN(theForm.amountTextfield.value)) {
        reason += "Amount Field must be a number in dollars. \n"
        theForm.amountTextfield.style.background = 'Yellow';
    }

    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    return true;
}

function validateFYForm(theForm) {
    var reason = "";
    reason += validateDropdownSet(theForm.fyDropdown);

    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    return true;
}

function validateSensor(theForm) {
    var reason = "";
    //needs an acronym
    reason += validateEmpty(theForm.acronymTextField);
    //needs a name
    reason += validateEmpty(theForm.nameTextField);
    //user has to select a country
    reason += validateEmpty(theForm.startTextField);
    reason += validateEmpty(theForm.eolTextField);
    reason += validateDropdownSet(theForm.agencyDropdown);

    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    return true;
}

function validateAgency(theForm) {
    var reason = "";
    //needs an acronym
    reason += validateEmpty(theForm.acronymTextField);
    //needs a name
    reason += validateEmpty(theForm.nameTextField);
    //user has to select a country
    reason += validateEmpty(theForm.countryTextField);

    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    return true;
}

function validateProgramme(theForm) {
    var reason = "";
    //needs an acronym
    reason += validateEmpty(theForm.acronymTextField);
    //needs a name
    reason += validateEmpty(theForm.nameTextField);
    //user needs to select a valid date
    reason += validateEmpty(theForm.satLaunchDatepicker);
    //user needs to select a valid eol year
    reason += validateEmpty(theForm.eolTextField);
    //user needs to select at least one agency
    if (!isAtLeastOneItemChecked("checkbox", 500)) {
        reason += "At least 1 Agency must be selected \n";
    }
    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    return true;
}

function validateSatellite(theForm) {
    var reason = "";
    //needs an acronym
    reason += validateEmpty(theForm.acronymTextField);
    //needs a name
    reason += validateEmpty(theForm.nameTextField);
    //user has to select a programme
    reason += validateDropdownSet(theForm.programmeDropdown);
    //user needs to select a valid date
    reason += validateEmpty(theForm.satLaunchDatepicker);
    //user needs to select a valid eol year
    reason += validateEmpty(theForm.eolTextField);
    //user needs to select at least one agency
//    if (!isAtLeastOneItemAdded("agency", 100)) {
//        reason += "At least 1 agency must be selected \n";
//    }
    //user needs to select at least one sensor
    if (!isAtLeastOneItemChecked("checkbox", 500)) {
        reason += "At least 1 sensor must be selected \n";

    }
    //user has to select a status
    reason += validateDropdownSet(theForm.statusDropdown);

    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    //alert("Whats up");
    return true;
}

function validateNewRequirement(theForm) {
    var reason = "";
    reason += validateEmpty(theForm.textfieldReq);
    reason += validateEmpty(theForm.textfieldSource);
    reason += validateDropdownSet(theForm.fyDropdown);
    //at least 1 parameter needs to be selected
    //to-do need to see if at least one form item has the string checkbox in it
//    if (!isAtLeastOneParameterSelected()) {
//        reason += "At least one parameter must be selected";
//    }
    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    //alert("Whats up");
    return true;
}

function validateEditRequirement(theForm) {
    var reason = "";
    //user has to select a fy
    reason += validateDropdownSet(theForm.fiscalYearDropdown);
    //user has to select a req    
    //console.log("req dropdown "+theForm.reqDropdown.selectedIndex);
    reason += validateDropdownSet(theForm.reqDropdown);
    //at least 1 parameter needs to be selected
//    if (!isAtLeastOneParameterSelected()) {
//        reason += "At least one parameter must be selected";
//    }
    //source cannot be blank
    if (theForm.textfieldSource !== undefined) {
        reason += validateEmpty(theForm.textfieldSource);
    }
    //cannot modify priority
    //Fiscal Year must be valid for this requirement
    if (theForm.fyDropdown !== undefined) {
        reason += validateDropdownSet(theForm.fyDropdown);
    }
    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    //alert("Whats up");
    return true;
}

function isAtLeastOneItemAdded(name, numValues) {
    var curValue = "";
    for (var i = 0; i < numValues; i++) {
        curValue = name + i;
        //console.log(curValue);
        if (document.getElementById(curValue) !== null) {
            return true;
        }
    }
    return false;
}

function isAtLeastOneItemChecked(name, numValues) {
    var curValue = "";
    for (var i = 0; i < numValues; i++) {
        curValue = name + i;
        //console.log(curValue);
        if (document.getElementById(curValue) !== null && document.getElementById(curValue).checked) {
            return true;
        }
    }
    return false;
}

function isAtLeastOneParameterSelected() {
    var curParam = "";
    for (var i = 0; i < 100; i++) {
        curParam = "checkbox" + i;
        if (document.getElementById(curParam) !== null && document.getElementById(curParam).checked) {
            return true;
        }
    }
    return false;
}

function validate1498(theForm) {
    var reason = "";

    //do date verification
    reason += validateDates(theForm.textFieldDateOfSummary, theForm.textFieldStartDate, theForm.textFieldEndDate, theForm.textFieldDateOfPrevSummary);
    //verify required fields are not blank
    reason += validateEmpty2(theForm.textFieldSummarySecurity);
    reason += validateEmpty2(theForm.textFieldWorkSecurity);
    reason += validateEmpty2(theForm.textFieldTitle);
    //reason += validateDropdownSet(theForm.piDropdown);
    //reason += validateEmpty(theForm.textFieldWbsID);
    //reason += validateEmpty(theForm.textFieldWbsDescription);
    reason += validateEmpty2(theForm.piNameTextField);
    reason += validateEmpty2(theForm.piPhoneTextField);
    reason += validateEmpty2(theForm.piEmailTextField);
    //verify wbs

    if (reason != "") {
        alert("Some fields need correction:\n\n" + reason);
        return false;
    }
    //alert("Whats up");
    return true;
}

function validateTask(theForm) {
    var reason = "";
    reason += validateEmpty(theForm.titleTextfield);
    reason += validateEmpty(theForm.descriptionTextfield);
    if (reason != "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    //alert("Whats up");
    return true;
}

function validateSubtask(theForm) {
    var reason = "";

    //empty fields
    reason += validateDropdownSet(theForm.reqDropdown);
    reason += validateDropdownSet(theForm.dropdownParameter);
    reason += validateEmpty(theForm.titleTextField);
    reason += validateEmpty(theForm.descriptionTextField);
    reason += validateEmpty(theForm.durationTextField);
    reason += validateEmpty(theForm.startDateTextField);
    reason += validateEmpty(theForm.endDateTextField);
    reason += validateEmpty(theForm.costTextField);

    if (theForm.satDropdown !== undefined && validateDropdownSet(theForm.satDropdown)) {
        reason += "A Satellite must be selected \n";
        theForm.satDropdown.style.background = 'Yellow';
    }
    else if (theForm.sensorDropdown !== undefined && validateDropdownSet(theForm.sensorDropdown)) {
        reason += "A Sensor must be selected \n";
        theForm.sensorDropdown.style.background = 'Yellow';
    }
    //duration
    if (isNaN(theForm.durationTextField.value)) {
        reason += "Duration Field must be a number of hours. \n"
        theForm.durationTextfield.style.background = 'Yellow';
    }
    else {
        if (theForm.durationTextField.value > 400) {
            reason += "Duration Field must be less than 40 hours. \n"
            theForm.durationTextfield.style.background = 'Yellow';
        }
    }
    //cost
    if (isNaN(theForm.costTextField.value)) {
        reason += "Cost must be a valid dollar amount. \n"
        theForm.costTextField.style.background = 'Yellow';
    }

    //is the start date before the end date
    if (isFirstDateBeforeSecond(theForm.endDateTextField.value, theForm.startDateTextField.value)) {
        reason += "End Date is before Start Date \n";
        theForm.endDateTextField.style.background = 'Yellow';
    }
    //is the start date before the project start date
    if (isFirstDateBeforeSecond(theForm.startDateTextField.value, convertDbDateToJSDate(theForm.projectStartDate.value))) {
        reason += "Subtask starts before project Start Date " + convertDbDateToJSDate(theForm.projectStartDate.value) + " \n";
        theForm.startDateTextField.style.background = 'Yellow';
    }
    //is the end date before the project end date
    if (isFirstDateBeforeSecond(theForm.endDateTextField.value, convertDbDateToJSDate(theForm.projectStartDate.value))) {
        reason += "Subtask ends before project Start Date " + convertDbDateToJSDate(theForm.projectStartDate.value) + "\n";
        theForm.endDateTextField.style.background = 'Yellow';
    }
    //is the project end date before the end date
    if (isFirstDateBeforeSecond(convertDbDateToJSDate(theForm.projectEndDate.value), theForm.endDateTextField.value)) {
        reason += "Subtask ends after project End Date " + convertDbDateToJSDate(theForm.projectEndDate.value) + "\n";
        theForm.endDateTextField.style.background = 'Yellow';
    }
    //is the project end date before the start date
    if (isFirstDateBeforeSecond(convertDbDateToJSDate(theForm.projectEndDate.value), theForm.startDateTextField.value)) {
        reason += "Subtask starts after project End Date " + convertDbDateToJSDate(theForm.projectEndDate.value) + "\n";
        theForm.startDateTextField.style.background = 'Yellow';
    }
    //reason += "ProjectStart: "+theForm.projectStartDate.value+" \n";
    //reason += "ProjectEnd: "+theForm.projectEndDate.value+" \n";
    //
    //performer(s)


    if (reason !== "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }
    //alert("Whats up");
    return true;
}

function doesAtLeastOnePerformerExist() {
    var performerExists = false;
    for (var i = 0; i < 6; i++) {
        if (document.getElementById("nameField" + i) !== null) {
            performerExists = true;
            break;
        }
    }
    return performerExists;
}

function validateDropdownSet(dropdown) {
    var output = "";
    if (dropdown === undefined) {
        output = "Select all required dropdown Fields \n";
    }
    else if (dropdown.selectedIndex === 0) {
        dropdown.style.background = 'Yellow';
        output = dropdown.name + " not selected \n";
    }

    else {
        dropdown.style.background = 'White';
    }
    return output;
}

function validateDates(submitDate, startDate, endDate, prevDate) {
    var output = "";

    //check empty fields first
    output += validateEmpty(submitDate);
    output += validateEmpty(startDate);
    output += validateEmpty(endDate);
    if (isFirstDateBeforeSecond(startDate.value, submitDate.value)) {
        output += "Submit Date is after Project Start Date \n";
        submitDate.style.background = 'Yellow';
    }
    if (isFirstDateBeforeSecond(endDate.value, submitDate.value)) {
        output += "Submit Date is after Project End Date \n";
        submitDate.style.background = 'Yellow';
    }
    if (isFirstDateBeforeSecond(endDate.value, startDate.value)) {
        output += "End Date is before Project Start date \n";
        endDate.style.background = 'Yellow';
    }
    if (prevDate.value === "") {
        //not required so set a default value
        prevDate.value = "00/00/0000";
    }
    return output;
}

function isFirstDateBeforeSecond(date1, date2) {
    //input should be mm/dd/YYYY
    var newDate1 = new Date(date1).getTime();
    var newDate2 = new Date(date2).getTime();

    if (newDate1 < newDate2) {
        return true;
    }
    else {
        return false;
    }
}

function convertDbDateToJSDate(dateString) {
    //in YYYY-mm-dd
    var pieces = dateString.split("-");
    //ouy mm/dd/YYYY
    return pieces[1] + '/' + pieces[2] + '/' + pieces[0];
}

function validateEmpty2(fld) {
    var output = "";
    if (fld.value.length === 0) {
        fld.style.background = 'Yellow';
        output = fld.id + " is blank \n";
    } else {
        fld.style.background = 'White';
    }
    return output;
}

function validateEmpty(fld) {
    var output = "";
    if (fld.value.length === 0) {
        fld.style.background = 'Yellow';
        output = fld.name + " is blank \n";
    } else {
        fld.style.background = 'White';
    }
    return output;
}


